<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${config.packageMapper}.${tableInfo.javaName}${config.suffixMapper}">
    <resultMap id="BaseResultMap" type="${config.packageEntity}.${tableInfo.javaName}${config.suffixEntity}">
    <#list tableInfo.columns as col>
        <result column="${col.dbName}" property="${col.javaName}" jdbcType="${col.dbType}"/>
    </#list>
    </resultMap>

    <sql id="Base_Column_List">
        <#list tableInfo.columns as col>
        ${col.dbName}<#if col?has_next>,</#if>
        </#list>
    </sql>

    <!-- 查询全字段列表 -->
    <select id="selectList" resultType="${config.packageEntity}.${tableInfo.javaName}${config.suffixEntity}">
        SELECT
        <#list tableInfo.columns as col>
            ${col.dbName}<#if col?has_next>,</#if>
        </#list>
        FROM ${tableInfo.dbName}
        <where>
        <#list tableInfo.columns as col>
            <if test="${col.javaName} != null" >
                AND ${col.dbName} = ${'#'}{${col.javaName}}
            </if>
        </#list>
        </where>
    </select>

    <!-- 查询全字段对象 -->
    <select id="getBy${utils.getUpperCamel(tableInfo.primaryKey)}" resultType="${config.packageEntity}.${tableInfo.javaName}${config.suffixEntity}">
        SELECT
        <#list tableInfo.columns as col>
            ${col.dbName}<#if col?has_next>,</#if>
        </#list>
        FROM ${tableInfo.dbName}
        <where>
            <#list tableInfo.primaryKeyColumns as col>
            ${col.dbName} = ${'#'}{${col.javaName}}<#if col?has_next>
            and </#if>
            </#list>
        </where>
    </select>

    <!-- 插入数据-->
    <insert id="insert" parameterType="${config.packageEntity}.${tableInfo.javaName}${config.suffixEntity}">
        INSERT INTO ${tableInfo.dbName}(
        <#list tableInfo.columns as col>
        <if test="${col.javaName} != null" >
            ${col.dbName}<#if col?has_next>,</#if>
        </if>
        </#list>
        ) VALUES (
        <#list tableInfo.columns as col>
        <if test="${col.javaName} != null" >
            ${'#'}{${col.javaName}}<#if col?has_next>,</#if>
        </if>
        </#list>
        )
    </insert>

    <#assign pkNames=utils.getDbNameListByColumn(tableInfo.primaryKeyColumns) />
    <#if pkNames?? && (pkNames?size>0)>
    <!-- 更新数据 -->
    <update id="update" parameterType="${config.packageEntity}.${tableInfo.javaName}${config.suffixEntity}">
        UPDATE ${tableInfo.dbName}
        <set>
        <#list tableInfo.columns as col>
            <#if !pkNames?seq_contains(col.dbName)>
            <if test="${col.javaName} != null" >
                ${col.dbName} = ${'#'}{${col.javaName}},
            </if>
            </#if>
        </#list>
        </set>
        <where>
            <#list tableInfo.primaryKeyColumns as col>
            ${col.dbName} = ${'#'}{${col.javaName}}
            </#list>
        </where>
    </update>
    </#if>

</mapper>